# coding: UTF-8
# @Date    ：2024/10/11 15:57 
# @File    : __init__.py
# :Author:  fum
import os

import colorama
from colorama import Fore, Back, Style
from tabulate import tabulate
from typing import List, Dict, Optional, Any

from api.fm.libs.file import TextFileUtils

# 初始化 colorama
colorama.init(autoreset=True)


class PrintUtils:
    """
    # 示例数据
    data = [
        {'Name': 'Alice', 'Age': 30, 'City': 'New York'},
        {'Name': 'Bob', 'Age': 25, 'City': 'Los Angeles'},
        {'Name': 'Charlie', 'Age': 35, 'City': 'Chicago'}
    ]

    # 使用 PrintUtils 类
    PrintUtils.print_colored("Hello, World!", color=Fore.GREEN, style=Style.BRIGHT)
    PrintUtils.print_fancy_data(data, title="用户数据", color=Fore.CYAN, style=Style.BRIGHT)

    # 测试新方法
    PrintUtils.print_success("操作成功！")
    PrintUtils.print_warning("这是一个警告消息！")
    PrintUtils.print_error("发生了错误！")
    PrintUtils.print_list(["项目一", "项目二", "项目三"], title="项目列表")
    """

    @staticmethod
    def print_colored(text: str, color: str = Fore.GREEN, background: str = Style.BRIGHT,
                      style: str = Style.NORMAL) -> None:
        """打印带有指定颜色的文本"""
        print(f"{style}{background}{color}{text}")

    @staticmethod
    def print_title(title: str, color: str = Fore.CYAN, style: str = Style.BRIGHT) -> None:
        """打印带有颜色的标题"""
        PrintUtils.print_colored(f"=== {title} ===", color, background=Back.BLACK, style=style)

    @staticmethod
    def print_table(data: List[List[Any]], headers: List[str], tablefmt: str = 'grid') -> None:
        """打印表格数据"""
        print(tabulate(data, headers=headers, tablefmt=tablefmt))

    @staticmethod
    def print_fancy_data(data: List[Dict[str, Any]], title: Optional[str] = None, color: str = Fore.CYAN,
                         style: str = Style.BRIGHT) -> None:
        """打印带有标题和颜色的表格数据"""
        if title:
            TEMP_file = os.path.join(os.environ['TEMP_DIR'], 'temp.json')
            json_data = {}
            if os.path.exists(TEMP_file):
                json_data = TextFileUtils.read_json(TEMP_file)
            if json_data and json_data.get(title, 0):
                return
            PrintUtils.print_title(title, color, style)
            json_data[title] = 1
            TextFileUtils.write_json(TEMP_file, json_data)
        if data:
            headers = list(data[0].keys())
            rows = [list(row.values()) for row in data]
            print(tabulate(rows, headers=headers, tablefmt='grid'))

    @staticmethod
    def print_success(message: str) -> None:
        """打印成功消息"""
        PrintUtils.print_colored(message, color=Fore.GREEN, style=Style.BRIGHT)

    @staticmethod
    def print_warning(message: str) -> None:
        """打印警告消息"""
        PrintUtils.print_colored(message, color=Fore.YELLOW, style=Style.BRIGHT)

    @staticmethod
    def print_error(message: str) -> None:
        """打印错误消息"""
        PrintUtils.print_colored(message, color=Fore.RED, style=Style.BRIGHT)

    @staticmethod
    def print_list(items: List[str], title: Optional[str] = None, color: str = Fore.CYAN,
                   style: str = Style.BRIGHT) -> None:
        """打印带有标题的列表"""
        if title:
            PrintUtils.print_title(title, color, style)
        for item in items:
            print(f"• {item}")


if __name__ == '__main__':
    data = [
        {'Name': 'Alice', 'Age': 30, 'City': 'New York'},
        {'Name': 'Bob', 'Age': 25, 'City': 'Los Angeles'},
        {'Name': 'Charlie', 'Age': 35, 'City': 'Chicago'}
    ]

    # 使用 PrintUtils 类
    PrintUtils.print_colored("Hello, World!", color=Fore.GREEN, style=Style.BRIGHT)
    PrintUtils.print_fancy_data(data, title="用户数据", color=Fore.CYAN, style=Style.BRIGHT)

    # 测试新方法
    PrintUtils.print_success("操作成功！")
    PrintUtils.print_warning("这是一个警告消息！")
    PrintUtils.print_error("发生了错误！")
    PrintUtils.print_list(["项目一", "项目二", "项目三"], title="项目列表")
